﻿using System;
using System.Text.RegularExpressions;

namespace Atata
{
    /// <summary>
    /// Provides a set of methods for URI manipulations.
    /// </summary>
    public static class UriUtils
    {
        private static readonly Regex UrlSchemaRegex = new Regex("^[a-z]+://");

        /// <summary>
        /// Tries to create an absolute <see cref="Uri"/>.
        /// </summary>
        /// <param name="urlString">The URL string.</param>
        /// <param name="result">The result containing constructed <see cref="Uri"/>.</param>
        /// <returns>
        /// A <see cref="bool"/> value that is <see langword="true"/> if the <see cref="Uri"/> was successfully created;
        /// otherwise, <see langword="false"/>.
        /// </returns>
        public static bool TryCreateAbsoluteUrl(string urlString, out Uri result)
        {
            if (urlString != null && UrlSchemaRegex.IsMatch(urlString))
            {
                return Uri.TryCreate(urlString, UriKind.Absolute, out result);
            }
            else
            {
                result = null;
                return false;
            }
        }

        /// <summary>
        /// Concatenates the specified base URI with the relative URI.
        /// </summary>
        /// <param name="baseUri">The base URI.</param>
        /// <param name="relativeUri">The relative URI.</param>
        /// <returns>The created <see cref="Uri"/>.</returns>
        public static Uri Concat(string baseUri, string relativeUri)
        {
            string fullUrl = baseUri;

            if (!string.IsNullOrWhiteSpace(relativeUri))
            {
                if (baseUri.EndsWith("/") && relativeUri.StartsWith("/"))
                    fullUrl += relativeUri.Substring(1);
                else if (!baseUri.EndsWith("/") && !relativeUri.StartsWith("/"))
                    fullUrl += "/" + relativeUri;
                else
                    fullUrl += relativeUri;
            }

            return new Uri(fullUrl);
        }
    }
}
